package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.model.Address;
import com.cskaoyan.model.AddressExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.AddressDataVO;

import com.cskaoyan.service.AddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：huzhouming
 * @description：实现用户管理收货地址部分具体操作
 * @date ：2022/5/5 22:48
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressMapper addressMapper;

    @Transactional
    @Override
    /**
     * @author: huzhouming
     * @description: 按userId, 收货人name查找
     * @date: 2022/5/5
     * @param baseParamVO
     * @param id
     * @param name
     * @return com.cskaoyan.model.vo.UserDataVO
     */
    public AddressDataVO query(BaseParamBO baseParamVO, Integer userId, String name) {
        Integer page = baseParamVO.getPage();//页码
        Integer limit = baseParamVO.getLimit();//数据量
        String sort = baseParamVO.getSort();//排序列
        String order = baseParamVO.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息

        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (userId != null ) {
           criteria.andUserIdEqualTo(userId);
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
            criteria.andDeletedEqualTo(false);
            addressExample.setOrderByClause(sort + " " + order);

        List<Address> list = addressMapper.selectByExample(addressExample);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Address> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return AddressDataVO.data(total, pages, limit, page, list);
    }
}
